package cn.bocweb.spinning.view.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.view.adapter.CollectAdapter;
import cn.bocweb.spinning.view.common.BaseActivity;
import cn.bocweb.spinning.view.fragment.CollectInfoFragment;
import cn.bocweb.spinning.view.fragment.CollectTopicFragment;

public class MyCollectActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.tabLayout)
    TabLayout tabLayout;
    @Bind(R.id.viewPager)
    ViewPager viewPager;

    CollectAdapter adapter;
    @Bind(R.id.my_collect_delete)
    TextView myCollectDelete;

    List<Fragment> fragments;
    CollectInfoFragment collectInfoFragment;
    CollectTopicFragment mCollectTopicFragment;
    boolean isInfoDeleteCancelShow = false;
    boolean isCollectDeleteCancelShow = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_collect);
        ButterKnife.bind(this);

        initToolbar();
        initFragment();
        initTab();
        initClickBtn();

    }

    private void initClickBtn() {
        myCollectDelete.setOnClickListener(this);
    }

    private void initTab() {
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setSelectedTabIndicatorColor(getResources().getColor(R.color.colorPrimary));
    }

    private void initToolbar() {
        toolbar.setTitle("收藏");
        toolbar.setTitleTextColor(getResources().getColor(R.color.white));
        toolbar.setNavigationIcon(R.mipmap.btn_exit);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initFragment() {
        collectInfoFragment = new CollectInfoFragment();
        mCollectTopicFragment = new CollectTopicFragment();
        fragments = new ArrayList<>();
        fragments.add(collectInfoFragment);
//        fragments.add(new CollectProductFragment());
//        fragments.add(new CollectCompanyFragment());
        fragments.add(mCollectTopicFragment);
        adapter = new CollectAdapter(getSupportFragmentManager(), fragments);
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0: {
                        if (!isInfoDeleteCancelShow) {
                            collectInfoFragment.clearAdapter();
                            isInfoDeleteCancelShow = false;
                            myCollectDelete.setText("删除");

                        } else {
                            collectInfoFragment.deleteAdapter();
                            myCollectDelete.setText("取消");
                            isInfoDeleteCancelShow = true;
                        }
                        break;
                    }
                    case 1: {
                        if (!isCollectDeleteCancelShow) {
                            mCollectTopicFragment.clearAdapter();
                            isCollectDeleteCancelShow = false;
                            myCollectDelete.setText("删除");

                        } else {
                            mCollectTopicFragment.deleteAdapter();
                            myCollectDelete.setText("取消");
                            isCollectDeleteCancelShow = true;
                        }
                        break;
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.my_collect_delete: {
                switch (viewPager.getCurrentItem()) {
                    case 0: {
                        if (isInfoDeleteCancelShow) {
                            collectInfoFragment.clearAdapter();
                            isInfoDeleteCancelShow = false;
                            myCollectDelete.setText("删除");

                        } else {
                            collectInfoFragment.deleteAdapter();
                            myCollectDelete.setText("取消");
                            isInfoDeleteCancelShow = true;
                        }
                        break;
                    }
                    case 1: {
                        if (isCollectDeleteCancelShow) {
                            mCollectTopicFragment.clearAdapter();
                            isCollectDeleteCancelShow = false;
                            myCollectDelete.setText("删除");

                        } else {
                            mCollectTopicFragment.deleteAdapter();
                            myCollectDelete.setText("取消");
                            isCollectDeleteCancelShow = true;
                        }
                        break;
                    }
                }
                break;
            }
        }
    }
}
